//
//  UploadVideoViewController.m
//  kankanVideo
//
//  Created by wangbo on 6/15/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "UploadVideoViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "MyRequest.h"
#import "AppDelegate.h"
#import "ASIHTTPRequest.h"


@implementation UploadVideoViewController
@synthesize tblView;
@synthesize btnSubmit;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
/*-(id)init{
    self=[super init];
    NSLog(@"uploadview_init");
    tblView.delegate=self;
    return self;
}*/
-(void)dealloc{
    [tblView release];
    [btnSubmit release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    NSLog(@"uploadview_viewdidload");
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    myview.layer.cornerRadius=7;
    myview.contentSize=CGSizeMake(300, 430);
    tblView.delegate=self;
    tblView.dataSource=self;
    [tblView reloadData];
}

- (void)viewDidUnload
{
    NSLog(@"uploadview_viewdidunload");
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)btnExitTapped:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)btnSubmitTapped:(id)sender{
    //upload video and form data;
    if(uploadstatus==0){
        AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
        [app showAlert:@"正在上传"];
        uploadstatus=1;
        MyRequest *request=[[[MyRequest alloc]init]autorelease];
        [request uploadVideoWithVideoURL:uploadArea.videoAdress forObject:self];
    }
    /*NSMutableArray *_arr=[[NSMutableArray alloc]init];
    [_arr addObject:lblTitle.text];
    [_arr addObject:lblContent.text];
    [_arr addObject:lblTag.text];
    MyRequest *request=[[MyRequest alloc]init];
    int result=[request uploadVideoDataWithVideoURL:uploadArea.videoAdress withMetaData:_arr];
    if(result==1){
        NSLog(@"perfect!!!!");
        [app showAlert:@"上传成功"];
        [app hideAlert];
    }else{
        NSLog(@"error in uploading!!!!");
        [app showAlert:@"上传失败"];
        [app hideAlert];
    }*/
}
-(void)requestFinishedHandler:(ASIHTTPRequest*)request{
    if(uploadstatus==1){
        NSLog(@"receive_upload_1:%@",request.responseString);
        uploadstatus=2;
        NSMutableArray *_arr=[[NSMutableArray alloc]init];
        [_arr addObject:lblTitle.text];
        [_arr addObject:lblContent.text];
        [_arr addObject:lblTag.text];
        NSString *res=[[NSString alloc]initWithString:request.responseString];
        [_arr addObject:res];
        MyRequest *request=[[[MyRequest alloc]init]autorelease];
        [request uploadVideoWithData:_arr forObject:self];
    }else if(uploadstatus==2){
        NSLog(@"receive_upload_2");
        if([request.responseString isEqualToString:@"1"]){
            AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
            [app showAlert:@"上传成功"];
            [self performSelector:@selector(myHideAlert) withObject:self afterDelay:2];
            uploadstatus=0;
        }else{
            AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
            [app showAlert:@"上传失败"];
            [self performSelector:@selector(myHideAlert) withObject:self afterDelay:2];
            uploadstatus=0;
        }
    }
}
-(void)myHideAlert{
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app hideAlert];
}
-(void)requestFailedHandler:(ASIHTTPRequest *)request{
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app showAlert:@"上传失败"];
    [app hideAlert];
    uploadstatus=0;
    //NSLog(@"%@",err);
    //NSLog(@"failed");
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"textfieldreturn");
    //myview.contentSize=CGSizeMake(300, 430);
    //[myview setFrame:CGRectMake(10, 15, 300, 430)];
    [textField resignFirstResponder];
    return YES;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    NSLog(@"section:%d",section);
    if(section==1)return 2;
    else return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        return 120;
    }else{
        if(indexPath.row==1)return 70;
        else return 35;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    //cell.textLabel.text=[NSString stringWithFormat:@"cell:%d",indexPath.row];
    //UITableViewCell *cell=[[[UITableViewCell alloc]init]autorelease];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if (indexPath.section==0) {
        uploadArea=[[UploadArea alloc]initWithFrame:CGRectMake(0, 0, 280, 120)];
        [cell.contentView addSubview:uploadArea];
        [uploadArea release];
    }else if(indexPath.section==1){
        if(indexPath.row==0){
            if(!lblTitle)lblTitle=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 260, 35)];
            [cell.contentView addSubview:lblTitle];
            [lblTitle release];
            lblTitle.delegate=self;
            lblTitle.placeholder=@"标题(必填)";
        }else if(indexPath.row==1){
            if(!lblContent)lblContent=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 260, 35)];
            [cell.contentView addSubview:lblContent];
            lblContent.delegate=self;
            lblContent.text=@"来自iPhone的原创视频";
            [lblContent release];
        }
    }else{
        if(!lblTag)lblTag=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 260, 35)];
        [cell.contentView addSubview:lblTag];
        lblTag.delegate=self;
        lblTag.text=@"原创";
        [lblTag release];
    }
    return cell;
}

@end
